<?php
include_once("../module/product/includes/plugin_order_class.php");
include_once("../module/product/includes/plugin_pro_class.php");
include("../lang/cn/company_type_config.php");
include_once("$config[webroot]/lib/Httpsqs.php");
$order = new order;

/*****ajax请求部分******/
if($_GET['act']=='getExpressNoRestNum' && $_GET['expressId']>0){
    $num = $order->getExpressNoRestNum($_GET['expressId']);
    exit(json_encode(array('num'=>$num)));
}
/***********/
//=======================
if($_POST['act']=='edit'&&$_POST['id']&&is_numeric($_POST['id']))
{	
    if(isset($_POST['is_before_pay']) && $_POST['is_before_pay']==1){//付款前编辑，修改订单商品单价
        if(!empty($_POST['sku_price'])){
            foreach ($_POST['sku_price'] as $sk=>$sv)
            {
                $sql = "update ".ORPRO." set price='{$sv}' where id='{$sk}'";
                $db->query($sql);
            }
        }
    }
	$time=time();
	$sql = "select title from ".LGSTEMP." where id = '$_POST[shipping_id]'";
	$db->query($sql);
	$ship = $db->fetchRow();
	
	$sql = "select payment_name,payment_type from ".PAYMENT." where payment_id='$_POST[payment_id]'";
	$db->query($sql);
	$pay = $db->fetchRow();
	
	if(!$_POST['cost']){
	    $_POST['cost'] = $_POST['money'];
	}
	$cost=$_POST['rebate']*1+$_POST['cost']*1;
	$money=$_POST['rebate']*1+$_POST['money']*1+$_POST['freight']*1;
	$shipstr=$str=$_POST['ship_name']?",ship_name='$_POST[ship_name]'":"";
	$shipstr.=$str.=$_POST['ship_addr']?",ship_addr='$_POST[ship_addr]'":"";
	$shipstr.=$str.=$_POST['ship_tel']?",ship_tel='$_POST[ship_tel]'":"";
	$shipstr.=$str.=$_POST['ship_zip']?",ship_zip='$_POST[ship_zip]'":"";
	$shipstr.=$str.=$_POST['ship_mobile']?",ship_mobile='$_POST[ship_mobile]'":"";
	$shipstr.=$str.=$_POST['shipping_idcard']?",shipping_idcard='$_POST[shipping_idcard]'":"";
	$shipstr.=$_POST['freight']?",money='$_POST[freight]'":"";
	$str.=$_POST['freight']?",freight='$_POST[freight]'":"";
	$str.=$_POST['weight']?",weight='$_POST[weight]'":"";
	$str.=$_POST['tax_price']?",tax_price='$_POST[tax_price]'":"";
	$str.=$_POST['insurance_fee']?",insurance_fee='$_POST[insurance_fee]'":"";
	$str.=empty($ship)?"":",shipping_name='$ship[title]'";
	$str.=empty($pay)?"":",payment_name='$pay[payment_name]'";
	$str.=empty($ship)?"":",shipping_id='$_POST[shipping_id]'";
	$str.=empty($pay)?"":",payment_id='$_POST[payment_id]'";
	$payment_type=$pay['payment_type']!="cod"?"online":"offline";
	
	$sql="update ".ORDER." set cost='$cost' $str where order_id='".$_POST['id']."'";
	$db->query($sql);
	
	//$sql="update ".ORDERDELIVERY." set shipping_id='$_POST[shipping_id]',shipping_name='$ship[title]' $shipstr where order_id='".$_POST['id']."'";
	//$db->query($sql);
	
	//$sql="update ".ORDERPAYMENT." set payment_id='$_POST[payment_id]',payment_name='$pay[payment_name]',payment_type='$payment_type',money='$money' $paystr where order_id='".$_POST['id']."'";
	//$db->query($sql);
	
	//$sql="update ".CASHFLOW." set price='-$money' where seller_email!='' and order_id ='".$_POST['id']."'";
	//$db->query($sql);
	
	//$sql="update ".CASHFLOW." set price='$money' where buyer_email!='' and order_id ='".$_POST['id']."'";
	//$db->query($sql);
	
	$sql="SELECT id,name FROM ".ADMIN." WHERE user='$_SESSION[ADMIN_USER]'";
	$db->query($sql);
	$re=$db->fetchRow();
	$order->order_log($_POST['id'],'订单编辑','编辑',$re['id'],$re['name']);
	die;
}
if($_POST['act']=='pay'&&$_POST['id']&&is_numeric($_POST['id']))
{	
	$time=time();
	$sql = "select payment_name,payment_type from ".PAYMENT." where payment_id='$_POST[payment_id]'";
	$db->query($sql);
	$pay = $db->fetchRow();
	$payment_type=$pay['payment_type']!="cod"?"online":"offline";
	
	$order->set_order_statu($_POST['id'],2,$_SESSION['ADMIN_USER']);

	$sql="update ".ORDER." set payment_id='$_POST[payment_id]',payment_name='$pay[payment_name]' where order_id='".$_POST['id']."'";
	$db->query($sql);
	die;
}
if($_POST['act']=='ship'&&$_POST['id']&&is_numeric($_POST['id']))
{	
	$time=time();
	$express_info=explode('_', $_POST['shipping_express_id']);
	
	if($_POST['is_edit']=='0'){
	    if($express_info[0]=='19' || $express_info[0]=='27'){//小包自动分配运单号
	        $_POST['shipping_no'] = $order->getExpressNo(19);
	        if(empty($_POST['shipping_no'])){//如果没得到自动分配的运单号，则只是编辑订单
	            $_POST['is_edit']='1';
	        }
	    }
	}
	
	$str=$_POST['shipping_no']?"shipping_no='".trim($_POST[shipping_no])."'":"";
	$str.=$_POST['shipping_weight']?",shipping_weight='$_POST[shipping_weight]'":"";
	$str.=$_POST['shipping_freight']?",shipping_freight='$_POST[shipping_freight]'":"";
	$str.=$_POST['ship_name']?",ship_name='$_POST[ship_name]'":"";
	$str.=$_POST['ship_addr']?",ship_addr='$_POST[ship_addr]'":"";
	$str.=$_POST['ship_tel']?",ship_tel='$_POST[ship_tel]'":"";
	$str.=$_POST['ship_mobile']?",ship_mobile='$_POST[ship_mobile]'":"";
	$str.=$express_info[0]?",shipping_express_id='$express_info[0]'":"";
	$str.=$express_info[1]?",shipping_express_name='$express_info[1]'":"";
	$str = trim($str,",");

	$sql="update ".ORDER." set $str where order_id='".$_POST['id']."'";
	$db->query($sql);
	
	if($_POST['is_edit']=='0'){
		$order->set_order_statu($_POST['id'],3,$_SESSION['ADMIN_USER']);
    	$order->order_log($_POST['id'],'订单发货完成,物流单号:'.$_POST['shipping_no'].'','发货','',$_SESSION['ADMIN_USER']);
	}
	$order->order_log($_POST['id'],'发货信息编辑','发货','',$_SESSION['ADMIN_USER']);
		msg("?m=product&s=order.php");
		die;
}
//备注订单
if($_POST["act"]=='remark' && is_numeric($_POST['id'])){
	if($_POST['remark']!=''){
		$sql = "insert into haiou_order_remark (order_id,admin_name,text) values ('$_POST[id]','$_SESSION[ADMIN_USER]','$_POST[remark]')";
		$db->query($sql);
	}
	$order->order_log($_POST['id'],'备注',''.$_POST[remark].'','',$_SESSION['ADMIN_USER']);
	msg("?m=product&s=order.php");
	die;
}
//取消订单
if($_POST["act"]=='cancel' && is_numeric($_POST['id']))
{
	$time=time();
	$admin_remark=$_POST['other_reason']?$_POST['admin_remark']." ".$_POST['other_reason']:$_POST['admin_remark'];
	
	$sql="select status from ".ORDER." where order_id='$_POST[id]'";
	$db->query($sql);
	$de=$db->fetchRow();
	
	$order_statu = $order->set_order_statu($_POST['id'],0,$_SESSION['ADMIN_USER']);
	
	if($de['status'] == '3')
	{
		$sql="select * from ts_orders where lotnumber='$_POST[id]'";
		$db->query($sql);
		$res=$db->getRows();
		foreach ($res as $key=> $var)
    	{
			$date = date('Y-m-d',time());
			$time = strtotime($date);
			$sqlstr = "insert into ts_purchases (shopid, shop, productid, product, spec, unit, pricebuy, number, supplierid, supplier, memo, timeexpress,lotnumber,usedby,timestart,timeend,type,puser ) values ('$var[shopid]', '$var[shop]', '$var[productid]', '$var[product]', '$var[spec]', '$var[unit]', '$var[pricebuy]', '$var[number]','$var[supplierid]', '$var[supplier]', '', '$time','','','','','1','$_SESSION[ADMIN_USER]' )";
			$db->query($sqlstr);
		}
	}
	
	$sql="update ".ORDER." set admin_remark='$admin_remark',status='0',finished_time='$time' where order_id ='".$_POST['id']."'";
	$db->query($sql);
	
	
		msg("?m=product&s=order.php");
		die;
}
if($_POST["act"]=='reback'&&is_numeric($_POST['id']))
{
    $order_statu = $order->set_order_statu($_POST['id'],6,$_SESSION['ADMIN_USER']);
}

if($_POST["act"]=='getSellAmountByCond')
{
    $cond = str_replace("\'", "'", $_POST['cond']);
    $salesInfo = $order->getSalesInfo(0,0,0,$cond);
    $html = "<div style='height:200px;'>总金额:".$config['money'].$salesInfo."</div>";
    die($html);
}

if(!empty($_GET['type']) and !empty($_GET['orderid']) and is_numeric($_GET['orderid']))
{
	if($_POST['act'])
	{	
		unset($_GET['s']);
		unset($_GET['m']);
		$time=time();
		//修改
		if($_POST["act"]=='shipping' and is_numeric($_POST['id']))
		{
			$sql="update ".ORDER." set shipping_name='$_POST[shipping_name]',shipping_address='$_POST[t] $_POST[shipping_address]',shipping_tel='$_POST[shipping_tel]',shipping_company='$_POST[shipping_company]',shipping_code='$_POST[shipping_code]',status='3',shipping_status='2',shipping_time='$time' where order_id='".$_POST['id']."'";
			$db->query($sql);
			unset($_GET['oid']);
		}
		if($_POST["act"]=='cancel' and is_numeric($_POST['id']))
		{
			$admin_remark=$_POST['other_reason']?$_POST['admin_remark']." ".$_POST['other_reason']:$_POST['admin_remark'];
			
			$sql="update ".ORDER." set admin_remark='$admin_remark',status='0',finished_time='$time' where order_id ='".$_POST['id']."'";
			$db->query($sql);
			
			unset($_GET['oid']);
		}
		if($_POST["act"]=='receipt' and is_numeric($_POST['id']))
		{
			$sql="update ".ORDER." set status='4',finished_time='$time' where order_id ='".$_POST['id']."'";
			$db->query($sql);
			unset($_GET['oid']);
		}
		if ($_POST["act"] == 'edit_shipped') {
		    //保存编辑的已发货信息
		    $sql="select status from ".ORDER." where order_id='$_POST[id]'";
		    $db->query($sql);
		    $de=$db->fetchRow();
		    if ($de['status'] != 3) {
		        exit('禁止保存：此订单状态已不是“已发货”');
		    }
		    $express_info=explode('_', $_POST['shipping_express_id']);
		    $str=$_POST['shipping_no']?",shipping_no='$_POST[shipping_no]'":"";
        	$str.=$_POST['shipping_weight']?",shipping_weight='$_POST[shipping_weight]'":"";
        	$str.=$_POST['shipping_freight']?",shipping_freight='$_POST[shipping_freight]'":"";
        	$str.=$_POST['ship_name']?",ship_name='$_POST[ship_name]'":"";
        	$str.=$_POST['ship_mobile']?",ship_mobile='$_POST[ship_mobile]'":"";
        	$str.=$_POST['shipping_idcard']?",shipping_idcard='$_POST[shipping_idcard]'":"";
        	$str.=$_POST['ship_addr']?",ship_addr='$_POST[ship_addr]'":"";
        	$str.=$express_info[0]?",shipping_express_id='$express_info[0]'":"";
        	$str.=$express_info[1]?",shipping_express_name='$express_info[1]'":"";
        	$str = substr($str,1);
        	$sql="update ".ORDER." set $str where order_id='".$_POST['id']."'";
        	$db->query($sql);
        	if ($_POST['shipping_no'] != $_POST['shipping_no_old'] || $_POST['shipping_express_id_old'] != $express_info[0]) {
        	    //物流更改，重新订阅
        	    subscriberShipping($_POST[id]);
        	}
        	$order->order_log($_POST['id'],'编辑已发货订单完成,物流单号:'.$_POST['shipping_no'].'','编辑已发货订单','',$_SESSION['ADMIN_USER']);
        	msg("?m=product&s=order.php");
        	die;
		}
		msg("?m=product&s=order.php");
	}
	switch($_GET['type'])
	{
		case "edit":
		{
			$de=$order->shop_orderdetail($_GET['orderid']);
			$tpl->assign("de",$de);
			if($de['status']==1&&$de['pay_status']==1)
			{
				$sql = "select id,title from ".LGSTEMP;
				$db->query($sql);
				$ship = $db->getRows();
				$tpl->assign("ship",$ship);
				$sql = "select payment_id,payment_name from ".PAYMENT." where payment_type!='cards'";
				$db->query($sql);
				$pay = $db->getRows();
				$tpl->assign("pay",$pay);
			}
			if(isset($_GET['is_before_pay']) && $_GET['is_before_pay']==1){
			    $page="order_edit_before_pay.htm";
			}else{
			    $page="order_edit.htm";
			}
			break;
		}
		case "pay":
		{
			$de=$order->shop_orderdetail($_GET['orderid']);
			$tpl->assign("de",$de);
			if($de['pay_status']==1)
			{
				$sql = "select payment_id,payment_name from ".PAYMENT." where payment_type!='cards'";
				$db->query($sql);
				$pay = $db->getRows();
				$tpl->assign("pay",$pay);
			}
			$page="order_pay.htm";
			break;
		}
		case "ship":
		{
			$shipping_express=$order->get_express();
			$de=$order->shop_orderdetail($_GET['orderid']);
			/***税费***/
			$products = array();
			foreach ($de['product'] as $val)
			{
				if($val['is_stocked']>=0){//只算备货完成的
					$products[] = array('id'=>$val['product_id'],'num'=>$val['num']);
				}
			}
			$pro = new pro();
			$rate_price = $pro->get_products_rate_price($products);
			$tpl->assign('recommend_rate_price',$recommend_rate_price);
			$tpl->assign('rate_price',$rate_price['price']);
			/***税费***/
			$tpl->assign("de",$de);
			$tpl->assign("shipping_express",$shipping_express);
			$page="order_ship.htm";
			break;
		}
		case "finish":
		{
			$page="";
			break;
		}
		case "remark":
		{
			$page="order_remark.htm";
			break;
		}
		case "cancel":
		{
			$de=$order->shop_orderdetail($_GET['orderid']);
			$tpl->assign("de",$de);
			$page="order_cancel.htm";
			break;
		}
		case "edit_shipped":
		{
		    //编辑已发货订单
		    $shipping_express=$order->get_express();
			$de=$order->shop_orderdetail($_GET['orderid']);
			$tpl->assign("de",$de);
			$tpl->assign("shipping_express", $shipping_express);
			$page="order_edit_shipped.htm";
            break;
		}
		default:
		{
			break;
		}
	}
	$tpl->assign("config",$config);
	$tpl->assign("client_system",$client_system);
	$tpl->assign("client_platform",$client_platform);
	$tpl->display($page);
}

?>